package demo.jdbc;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

public class DemoJTable extends JFrame{
	
	private JTable tbData;
	
	public DemoJTable(){
		super("Demo JTable");
		initComponent();
		
		addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e){
				System.exit(0);
			}
		});
	}
	
	private void initComponent(){
		setBounds(10, 20, 210, 120);
		Container c = this.getContentPane();
		c.setLayout(new BorderLayout());
		tbData = new JTable();
		TableModel model = new DefaultTableModel(10, 3);
		
		try {
			Statement st = DataProvider.getConnection().createStatement();
			ResultSet rs = st.executeQuery("Select MaHS, Hoten, DTB FROM HocSinh");
			int row = 0;
			
			while(rs.next()){
				model.setValueAt(rs.getInt(1) + "", row, 0);
				model.setValueAt(rs.getString(2), row, 1);
				model.setValueAt(rs.getFloat(3) + "", row, 2);
				row++;
			}
			
			DataProvider.closeConnection();
			
			tbData.setModel(model);
			
			c.add(tbData, BorderLayout.CENTER);
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}